<?php

class Model_Mapper_ItemResource {
	
	protected $_dbTable;
	private static $instance;
	
	public static function newInstance() {
		if (! self::$instance instanceof self) {
			self::$instance = new self ();
		}
		return self::$instance;
	}
	
	public function setDbTable($dbTable) {
		if (is_string ( $dbTable )) {
			$dbTable = new $dbTable ();
		}
		if (! $dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception ( 'Invalid table data gateway provided' );
		}
		$this->_dbTable = $dbTable;
		return $this;
	}
	
	public function getDbTable() {
		if (null === $this->_dbTable) {
			$this->setDbTable ( 'Model_Table_ItemResource' );
		}
		return $this->_dbTable;
	}
	
	public function save(Model_ItemResource $item_resource) {
		$data = array ('item_id' => $item_resource->getItem_id (), 'name' => $item_resource->getName (), 'extension' => $item_resource->getExtension (), 'content_type' => $item_resource->getContent_type (), 'path' => $item_resource->getPath (), 'type' => $item_resource->getType () );
		$key = $this->getDbTable ()->getPrimaryKey ();
		$method = 'get' . ucfirst ( $key );
		$val = $item_resource->$method ();
		if ($val == null) {
			unset ( $data [$key] );
			$item_resource->setResource_id($this->getDbTable ()->insert ( $data ));
		} else {
			$this->getDbTable ()->update ( $data, array ($key . ' = ?' => $val ) );
		}
	}
	
	public function getItemPrimary($item, $array = false) {
		if ($item == '') {
			throw new Exception ( 'Empty item Provided.' );
		}
		if (! $item instanceof Model_Items) {
			$item = Model_Mapper_Items::newInstance ()->getByID ( $item );
		}
		$args = array ('item_id' => $item->getItem_id () );
		$args ['type'] = 'primary';
		if ($array) {
			$result = $this->getByCondition ( $args, true );
		} else {
			$result = $this->getByCondition ( $args );
		}
		
		if ($result) {
			return $result [0];
		}else{
			false;
		}
		
	
	}
	
	/*
     * *********************************
     * Gets Resources by Item ID
     * *********************************
     */
	
	public function getByItemID($item, $array = false) {
		if ($item == '') {
			throw new Exception ( 'Empty $item Provided.' );
		}
		if (! $item instanceof Model_Items) {
			$item = Model_Mapper_Items::newInstance ()->getByID ( $item );
		}
		
		$args = array ('item_id' => $item->getItem_id () );
		if ($array) {
			$result = $this->getByCondition ( $args, true );
		} else {
			$result = $this->getByCondition ( $args );
		}
		
		if ($result) {
			return $result;
		}
		return array ();
	}
	
	/*
	 * *******************************
	 * Get Items By Condition
	 * *******************************
	 */
	
	function getByCondition(array $args, $array = false) {
		if (! is_array ( $args )) {
			throw new Exception ( 'Invalid Argument Array Provided' );
		}
		$table = $this->getDbTable ();
		$select = $table->select ();
		$select->from ( $table );
		foreach ( $args as $key => $value ) {
			$select->where ( $key . '=?', $value );
		}
		
		$result = $table->fetchAll ( $select );
		$result = $this->extend ( $result );
		if ($result) {
			if ($array) {
				foreach ( $result as $key => $item ) {
					$result [$key] = $item->toArray ();
				}
			}
			
			return $result;
		
		} else {
			return array ();
		}
	}
	
	private function extend($args) {
		$items = array ();
		foreach ( $args as $arg ) {
			$arg = $arg->toArray ();
			$item = new Model_ItemResource ( $arg );
			$items [] = $item;
		}
		return $items;
	}
	
	public function lastInsertID() {
		return $this->getDbTable ()->getAdapter ()->lastInsertId ();
	}
	
	public function beginTransaction() {
		$this->getDbTable ()->getAdapter ()->beginTransaction ();
	}
	
	public function commit() {
		$this->getDbTable ()->getAdapter ()->commit ();
	}
	
	public function rollBack() {
		$this->getDbTable ()->getAdapter ()->rollBack ();
	}
	
	public function getAll($enabled = false) {
		$table = $this->getDbTable ();
		$select = $table->select ();
		$select->from ( $this->getDbTable () );
		if ($enabled) {
			$select->where ( 'enabled=?', 1 );
		}
		
		return $this->extend ( $table->fetchAll ( $select ) );
	}
	public function delete(array $args) {
		if (! is_array ( $args )) {
			throw new Exception ( '' );
		}
	
	}
	
	public function getByID($id) {
		if ($id == '') {
			throw new Exception ( 'Invalid ID Provided.' );
		}
		$table = $this->getDbTable ();
		$select = $table->select ();
		$select->where ( $this->getDbTable ()->getPrimaryKey () . '=?', $id );
		$select->from ( $this->getDbTable () );
		$result = $this->extend ( $table->fetchAll ( $select ) );
		return $result [0];
	}

}